﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.OleDb;

namespace 自消费
{
    [Serializable]
    public class TaskData
    {

        public const string CONTENTField = "@content";

        public const string ENABLEDField = "@enabled";

        public const string EX_SETTINGField = "@ex_setting";

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string INTERVALTIMEField = "@IntervalTime";

        public const string RUNTIMESField = "@RunTimes";

        public const string SEQNUMField = "@seqnum";

        public const string TASKGROUP_IDField = "@taskgroup_id";

        public const string TASKNAMEField = "@taskname";

        public const string TYPEField = "@type";

        private String _content;

        private Boolean _enabled;

        private String _ex_setting;

        private Int32 _ID;

        private Int32 _IntervalTime;

        private Int32 _RunTimes;

        private Int32 _seqnum;

        private Int32 _taskgroup_id;

        private String _taskname;

        private Int32 _type;

        public String content
        {
            get { return _content; }
            set { _content = value; }
        }
        public Boolean enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        public String ex_setting
        {
            get { return _ex_setting; }
            set { _ex_setting = value; }
        }
        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public Int32 IntervalTime
        {
            get { return _IntervalTime; }
            set { _IntervalTime = value; }
        }
        public Int32 RunTimes
        {
            get { return _RunTimes; }
            set { _RunTimes = value; }
        }
        public Int32 seqnum
        {
            get { return _seqnum; }
            set { _seqnum = value; }
        }
        public Int32 taskgroup_id
        {
            get { return _taskgroup_id; }
            set { _taskgroup_id = value; }
        }
        public String taskname
        {
            get { return _taskname; }
            set { _taskname = value; }
        }
        public Int32 type
        {
            get { return _type; }
            set { _type = value; }
        }
        public TaskData()
        {
            _content = "";
            _enabled = true;
            _ex_setting = "";
            _ID = 0;
            _IntervalTime = 0;
            _RunTimes = 1;
            _seqnum = 0;
            _taskgroup_id = 0;
            _taskname = "";
            _type = 0;
        }
    }

    public class TaskDataFactory
    {
        public static TaskData Construct(IDataReader reader)
        {
            TaskData data = new TaskData();

            try { data.content = Convert.ToString(reader["content"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(reader["enabled"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(reader["ex_setting"]); }
            catch { }

            try { data.ID = Convert.ToInt32(reader["ID"]); }
            catch { }

            try { data.IntervalTime = Convert.ToInt32(reader["IntervalTime"]); }
            catch { }

            try { data.RunTimes = Convert.ToInt32(reader["RunTimes"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(reader["seqnum"]); }
            catch { }

            try { data.taskgroup_id = Convert.ToInt32(reader["taskgroup_id"]); }
            catch { }

            try { data.taskname = Convert.ToString(reader["taskname"]); }
            catch { }

            try { data.type = Convert.ToInt32(reader["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Int32 GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dt.Rows.Count < rowIndex + 1)

                return 0;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return 0;

            try { PrimaryKey = Convert.ToInt32(dr[TaskData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static TaskData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static TaskData Construct(DataTable dt, int rowIndex)
        {
            TaskData data = new TaskData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.content = Convert.ToString(dr["content"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.IntervalTime = Convert.ToInt32(dr["IntervalTime"]); }
            catch { }

            try { data.RunTimes = Convert.ToInt32(dr["RunTimes"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.taskgroup_id = Convert.ToInt32(dr["taskgroup_id"]); }
            catch { }

            try { data.taskname = Convert.ToString(dr["taskname"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static TaskData Construct(DataGridView dgv, int rowIndex)
        {
            TaskData data = new TaskData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.content = Convert.ToString(dr["content"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.IntervalTime = Convert.ToInt32(dr["IntervalTime"]); }
            catch { }

            try { data.RunTimes = Convert.ToInt32(dr["RunTimes"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.taskgroup_id = Convert.ToInt32(dr["taskgroup_id"]); }
            catch { }

            try { data.taskname = Convert.ToString(dr["taskname"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dgv.Rows.Count < rowIndex + 1)

                return 0;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return 0;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == TaskData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToInt32(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static TaskData Construct(DataRow dr)
        {
            TaskData data = new TaskData();

            try { data.content = Convert.ToString(dr["content"]); }
            catch { }

            try { data.enabled = Convert.ToBoolean(dr["enabled"]); }
            catch { }

            try { data.ex_setting = Convert.ToString(dr["ex_setting"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.IntervalTime = Convert.ToInt32(dr["IntervalTime"]); }
            catch { }

            try { data.RunTimes = Convert.ToInt32(dr["RunTimes"]); }
            catch { }

            try { data.seqnum = Convert.ToInt32(dr["seqnum"]); }
            catch { }

            try { data.taskgroup_id = Convert.ToInt32(dr["taskgroup_id"]); }
            catch { }

            try { data.taskname = Convert.ToString(dr["taskname"]); }
            catch { }

            try { data.type = Convert.ToInt32(dr["type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataRow dr)
        {
            Int32 PrimaryKey = 0;

            try { PrimaryKey = Convert.ToInt32(dr[TaskData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class TaskDataDBOption
    {
        public static int Insert(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into Task (");
            sql.Append("content, ");
            sql.Append("enabled, ");
            sql.Append("ex_setting, ");
            sql.Append("IntervalTime, ");
            sql.Append("RunTimes, ");
            sql.Append("seqnum, ");
            sql.Append("taskgroup_id, ");
            sql.Append("taskname, ");
            sql.Append("type");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.content.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.enabled.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.ex_setting.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.IntervalTime.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.RunTimes.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.seqnum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.taskgroup_id.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.taskname.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.type.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update Task set ");
            sql.Append("content=");
            sql.Append("'");
            sql.Append(d.content.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("enabled=");
            sql.Append("");
            sql.Append(d.enabled.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("ex_setting=");
            sql.Append("'");
            sql.Append(d.ex_setting.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("IntervalTime=");
            sql.Append("");
            sql.Append(d.IntervalTime.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("RunTimes=");
            sql.Append("");
            sql.Append(d.RunTimes.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("seqnum=");
            sql.Append("");
            sql.Append(d.seqnum.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("taskgroup_id=");
            sql.Append("");
            sql.Append(d.taskgroup_id.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("taskname=");
            sql.Append("'");
            sql.Append(d.taskname.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("type=");
            sql.Append("");
            sql.Append(d.type.ToString());
            sql.Append("");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from Task ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Task ");
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Task ");
            return WFNetLib.ADO.AccessOP.DataSetSQL(sql.ToString());
        }
        public static OleDbDataReader ReaderSelect(TaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from Task ");
            return WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql.ToString());
        }
    }
}